package com.rxyb.hongyu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxyb.core.respone.R;
import com.rxyb.core.respone.enums.CommonCode;
import com.rxyb.dfs.FastDfsClient;
import com.rxyb.hongyu.constants.HongYuConstants;
import com.rxyb.hongyu.db.dao.ThirdHongyuPushMapper;
import com.rxyb.hongyu.db.po.ThirdHongyuCfg;
import com.rxyb.hongyu.db.po.ThirdHongyuPush;
import com.rxyb.hongyu.gateway.dto.HongYuReq;
import com.rxyb.hongyu.gateway.dto.HongYuRsp;
import com.rxyb.hongyu.gateway.dto.UpdateRecordReq;
import com.rxyb.hongyu.gateway.dto.UpdateRecordRsp;
import com.rxyb.hongyu.gateway.dto.pull.*;
import com.rxyb.hongyu.gateway.enums.RspCodeForHongyu;
import com.rxyb.hongyu.service.ThirdHongyuCfgService;
import com.rxyb.hongyu.service.ThirdHongyuPullService;
import com.rxyb.hongyu.service.logic.HongyuPullValidateLogic;
import com.rxyb.hongyu.service.logic.ThirdHongyuPullLogic;
import com.rxyb.hongyu.utils.HongYuClient;
import com.rxyb.toolize.image.ImageTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Base64;

/**
 * <p>
 * 鸿宇推送的记录 服务实现类
 * </p>
 *
 * @author DUYH
 * @since 2020-06-09
 */
@Slf4j
@Service
public class ThirdHongyuPullServiceImpl extends ServiceImpl<ThirdHongyuPushMapper, ThirdHongyuPush> implements ThirdHongyuPullService {

    @Autowired
    private HongYuClient hongYuClient;
    @Autowired(required = false)
    private FastDfsClient fastDfsClient;
    @Autowired
    private ThirdHongyuPullLogic thirdHongyuPullLogic;
    @Autowired
    private ThirdHongyuCfgService thirdHongyuCfgService;
    @Autowired
    private HongyuPullValidateLogic hongyuPullValidateLogic;

    @Override
    public HongYuRsp f20C01(HongYuReq hongYuReq) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        String reqDoc;
        if (HongYuConstants.teskKey.equals(hongYuReq.getJkxlh())) {
            log.debug("小杜测试，直接跳过验签。。。。。");
            reqDoc = hongYuReq.getReqDoc();
        } else {
            final ThirdHongyuCfg thirdHongyuCfg = thirdHongyuCfgService.queryByJkxlh(hongYuReq.getJkxlh());
            if (null == thirdHongyuCfg) {
                rsp.setRetcode(RspCodeForHongyu.NO_MCHNT.getCode());
                rsp.setRetmessage(RspCodeForHongyu.NO_MCHNT.getDesc());
                return rsp;
            }
            reqDoc = hongYuClient.getReqDoc(thirdHongyuCfg, hongYuReq);
        }
        // 请求报文
        F20C01Req req = JSON.parseObject(reqDoc, F20C01Req.class);
        // 校验请求报文
        HongYuRsp validate = hongyuPullValidateLogic.validate(req);
        //
        String apiNo = HongYuConstants.FUNC_20C01;
        String unionKey = hongyuPullValidateLogic.getApiKey(req);
        final Integer id = thirdHongyuPullLogic.saveHyApiRecord(apiNo, unionKey, req, rsp, validate);
        log.info("f20C01 调用业务接口");
        return rsp;
    }

    @Override
    public HongYuRsp f20C02(HongYuReq hongYuReq) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        String reqDoc;
        if (HongYuConstants.teskKey.equals(hongYuReq.getJkxlh())) {
            log.debug("小杜测试，直接跳过验签。。。。。");
            reqDoc = hongYuReq.getReqDoc();
        } else {
            final ThirdHongyuCfg thirdHongyuCfg = thirdHongyuCfgService.queryByJkxlh(hongYuReq.getJkxlh());
            if (null == thirdHongyuCfg) {
                rsp.setRetcode(RspCodeForHongyu.NO_MCHNT.getCode());
                rsp.setRetmessage(RspCodeForHongyu.NO_MCHNT.getDesc());
                return rsp;
            }
            reqDoc = hongYuClient.getReqDoc(thirdHongyuCfg, hongYuReq);
        }
        // 请求报文
        F20C02Req req = JSON.parseObject(reqDoc, F20C02Req.class);
        // 校验请求报文
        HongYuRsp validate = hongyuPullValidateLogic.validate(req);
        // 是否重复推送校验
        String apiNo = HongYuConstants.FUNC_20C02;
        String unionKey = hongyuPullValidateLogic.getApiKey(req);
        if (HongYuConstants.isSucc(rsp.getRetcode())) {
            int queryByUnionKey = thirdHongyuPullLogic.queryByUnionKey(unionKey, apiNo);
            if (queryByUnionKey > 0) {
                validate.setRetcode(RspCodeForHongyu.REPEAT.getCode());
                validate.setRetmessage(RspCodeForHongyu.REPEAT.getDesc());
            }
        }
        final Integer id = thirdHongyuPullLogic.saveHyApiRecord(apiNo, unionKey, req, rsp, validate);
        log.info("f20C02 调用业务接口");
        return rsp;
    }

    @Override
    public HongYuRsp f20C03(HongYuReq hongYuReq) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        String reqDoc;
        if (HongYuConstants.teskKey.equals(hongYuReq.getJkxlh())) {
            log.debug("小杜测试，直接跳过验签。。。。。");
            reqDoc = hongYuReq.getReqDoc();
        } else {
            final ThirdHongyuCfg thirdHongyuCfg = thirdHongyuCfgService.queryByJkxlh(hongYuReq.getJkxlh());
            if (null == thirdHongyuCfg) {
                rsp.setRetcode(RspCodeForHongyu.NO_MCHNT.getCode());
                rsp.setRetmessage(RspCodeForHongyu.NO_MCHNT.getDesc());
                return rsp;
            }
            reqDoc = hongYuClient.getReqDoc(thirdHongyuCfg, hongYuReq);
        }
        // 请求报文
        F20C03Req req = JSON.parseObject(reqDoc, F20C03Req.class);
        // 校验请求报文
        HongYuRsp validate = hongyuPullValidateLogic.validate(req);
        // 是否重复推送校验
        String apiNo = HongYuConstants.FUNC_20C03;
        String unionKey = hongyuPullValidateLogic.getApiKey(req);
        if (HongYuConstants.isSucc(rsp.getRetcode())) {
            int queryByUnionKey = thirdHongyuPullLogic.queryByUnionKey(unionKey, apiNo);
            if (queryByUnionKey > 0) {
                validate.setRetcode(RspCodeForHongyu.REPEAT.getCode());
                validate.setRetmessage(RspCodeForHongyu.REPEAT.getDesc());
            }
        }
        final Integer id = thirdHongyuPullLogic.saveHyApiRecord(apiNo, unionKey, req, rsp, validate);
        log.info("f20C03 调用业务接口");
        return rsp;
    }

    @Override
    public HongYuRsp f20C04(HongYuReq hongYuReq) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        String reqDoc;
        if (HongYuConstants.teskKey.equals(hongYuReq.getJkxlh())) {
            log.debug("小杜测试，直接跳过验签。。。。。");
            reqDoc = hongYuReq.getReqDoc();
        } else {
            final ThirdHongyuCfg thirdHongyuCfg = thirdHongyuCfgService.queryByJkxlh(hongYuReq.getJkxlh());
            if (null == thirdHongyuCfg) {
                rsp.setRetcode(RspCodeForHongyu.NO_MCHNT.getCode());
                rsp.setRetmessage(RspCodeForHongyu.NO_MCHNT.getDesc());
                return rsp;
            }
            reqDoc = hongYuClient.getReqDoc(thirdHongyuCfg, hongYuReq);
        }
        // 请求报文
        F20C04Req req = JSON.parseObject(reqDoc, F20C04Req.class);
        // 校验请求报文
        HongYuRsp validate = hongyuPullValidateLogic.validate(req);
        // 图片上传保存，若图片保存失败，则返回鸿宇失败响应，借助对方的队列重新发送
        String apiNo = HongYuConstants.FUNC_20C04;
        try {
            byte[] imgbyte = java.util.Base64.getDecoder().decode(req.getSkpic());
            // 图片打马赛克，3为模块大小
            byte[] mskimgbyte = ImageTools.mosaic(imgbyte, "jpeg", 3);
            if (null == mskimgbyte) {
                log.error("实况图打马赛克异常");
                rsp.setRetcode(RspCodeForHongyu.SAVE_ERROR.getCode());
                rsp.setRetmessage(RspCodeForHongyu.SAVE_ERROR.getDesc());
                return rsp;
            }
            // 上传至DFS
            String url1 = fastDfsClient.upload2FastDFS(mskimgbyte, "jpeg");
            if (url1 == null) {
                log.error("保存实况图至DFS失败");
                rsp.setRetcode(RspCodeForHongyu.SAVE_ERROR.getCode());
                rsp.setRetmessage(RspCodeForHongyu.SAVE_ERROR.getDesc());
                return rsp;
            }
            req.setSkpic(url1);
        } catch (Exception e) {
            log.error("保存实况图至本地失败：【{}】", e);
            rsp.setRetcode(RspCodeForHongyu.SAVE_ERROR.getCode());
            rsp.setRetmessage(RspCodeForHongyu.SAVE_ERROR.getDesc());
            return rsp;
        }
        final Integer id = thirdHongyuPullLogic.saveHyApiRecord(apiNo, apiNo, req, rsp, validate);
        log.info("f20C04 调用业务接口");
        return rsp;
    }

    @Override
    public HongYuRsp f20C05(HongYuReq hongYuReq) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        String reqDoc;
        if (HongYuConstants.teskKey.equals(hongYuReq.getJkxlh())) {
            log.debug("小杜测试，直接跳过验签。。。。。");
            reqDoc = hongYuReq.getReqDoc();
        } else {
            final ThirdHongyuCfg thirdHongyuCfg = thirdHongyuCfgService.queryByJkxlh(hongYuReq.getJkxlh());
            if (null == thirdHongyuCfg) {
                rsp.setRetcode(RspCodeForHongyu.NO_MCHNT.getCode());
                rsp.setRetmessage(RspCodeForHongyu.NO_MCHNT.getDesc());
                return rsp;
            }
            reqDoc = hongYuClient.getReqDoc(thirdHongyuCfg, hongYuReq);
        }
        // 请求报文
        F20C05Req req = JSON.parseObject(reqDoc, F20C05Req.class);
        // 校验请求报文
        HongYuRsp validate = hongyuPullValidateLogic.validate(req);
        // 是否重复推送校验
        String apiNo = HongYuConstants.FUNC_20C05;
        String unionKey = hongyuPullValidateLogic.getApiKey(req);
        if (HongYuConstants.isSucc(rsp.getRetcode())) {
            int queryByUnionKey = thirdHongyuPullLogic.queryByUnionKey(unionKey, apiNo);
            if (queryByUnionKey > 0) {
                validate.setRetcode(RspCodeForHongyu.REPEAT.getCode());
                validate.setRetmessage(RspCodeForHongyu.REPEAT.getDesc());
            }
        }
        try {
            // 上传至DFS
            String dfsUrl = fastDfsClient.upload2FastDFS(Base64.getDecoder().decode(req.getZppic()), "jpeg");
            if (dfsUrl == null) {
                log.info("保存审核驳回照片至DFS失败【{}】", req.getHphm());
                rsp.setRetcode(RspCodeForHongyu.SAVE_ERROR.getCode());
                rsp.setRetmessage(RspCodeForHongyu.SAVE_ERROR.getDesc());
                return rsp;
            }
            req.setZppic(dfsUrl);
        } catch (Exception e) {
            log.error("【{}】保存审核驳回照片异常：【{}】", req.getHphm(), e);
            rsp.setRetcode(RspCodeForHongyu.SAVE_ERROR.getCode());
            rsp.setRetmessage(RspCodeForHongyu.SAVE_ERROR.getDesc());
            return rsp;
        }
        final Integer id = thirdHongyuPullLogic.saveHyApiRecord(apiNo, unionKey, req, rsp, validate);
        log.info("f20C05 调用业务接口");
        return rsp;
    }

    @Override
    public HongYuRsp f20C06(HongYuReq hongYuReq) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        String reqDoc;
        if (HongYuConstants.teskKey.equals(hongYuReq.getJkxlh())) {
            log.debug("小杜测试，直接跳过验签。。。。。");
            reqDoc = hongYuReq.getReqDoc();
        } else {
            final ThirdHongyuCfg thirdHongyuCfg = thirdHongyuCfgService.queryByJkxlh(hongYuReq.getJkxlh());
            if (null == thirdHongyuCfg) {
                rsp.setRetcode(RspCodeForHongyu.NO_MCHNT.getCode());
                rsp.setRetmessage(RspCodeForHongyu.NO_MCHNT.getDesc());
                return rsp;
            }
            reqDoc = hongYuClient.getReqDoc(thirdHongyuCfg, hongYuReq);
        }
        // 请求报文
        F20C06Req req = JSON.parseObject(reqDoc, F20C06Req.class);
        // 校验请求报文
        HongYuRsp validate = hongyuPullValidateLogic.validate(req);
        // 是否重复推送校验
        String apiNo = HongYuConstants.FUNC_20C06;
        String unionKey = hongyuPullValidateLogic.getApiKey(req);
        if (HongYuConstants.isSucc(rsp.getRetcode())) {
            int queryByUnionKey = thirdHongyuPullLogic.queryByUnionKey(unionKey, apiNo);
            if (queryByUnionKey > 0) {
                validate.setRetcode(RspCodeForHongyu.REPEAT.getCode());
                validate.setRetmessage(RspCodeForHongyu.REPEAT.getDesc());
            }
        }
        final Integer id = thirdHongyuPullLogic.saveHyApiRecord(apiNo, unionKey, req, rsp, validate);
        log.info("f20C06 调用业务接口");
        return rsp;
    }

    @Override
    public HongYuRsp f20C07(HongYuReq hongYuReq) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        String reqDoc;
        if (HongYuConstants.teskKey.equals(hongYuReq.getJkxlh())) {
            log.debug("小杜测试，直接跳过验签。。。。。");
            reqDoc = hongYuReq.getReqDoc();
        } else {
            final ThirdHongyuCfg thirdHongyuCfg = thirdHongyuCfgService.queryByJkxlh(hongYuReq.getJkxlh());
            if (null == thirdHongyuCfg) {
                rsp.setRetcode(RspCodeForHongyu.NO_MCHNT.getCode());
                rsp.setRetmessage(RspCodeForHongyu.NO_MCHNT.getDesc());
                return rsp;
            }
            reqDoc = hongYuClient.getReqDoc(thirdHongyuCfg, hongYuReq);
        }
        // 请求报文
        F20C07Req req = JSON.parseObject(reqDoc, F20C07Req.class);
        // 校验请求报文
        HongYuRsp validate = hongyuPullValidateLogic.validate(req);
        //
        String apiNo = HongYuConstants.FUNC_20C07;
        final Integer id = thirdHongyuPullLogic.saveHyApiRecord(apiNo, apiNo, req, rsp, validate);
        log.info("f20C07 调用业务接口");
        return rsp;
    }

    @Override
    public HongYuRsp f20C08(HongYuReq hongYuReq) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode(RspCodeForHongyu.SUCCESS.getCode());
        rsp.setRetmessage(RspCodeForHongyu.SUCCESS.getDesc());
        String reqDoc;
        if (HongYuConstants.teskKey.equals(hongYuReq.getJkxlh())) {
            log.debug("小杜测试，直接跳过验签。。。。。");
            reqDoc = hongYuReq.getReqDoc();
        } else {
            final ThirdHongyuCfg thirdHongyuCfg = thirdHongyuCfgService.queryByJkxlh(hongYuReq.getJkxlh());
            if (null == thirdHongyuCfg) {
                rsp.setRetcode(RspCodeForHongyu.NO_MCHNT.getCode());
                rsp.setRetmessage(RspCodeForHongyu.NO_MCHNT.getDesc());
                return rsp;
            }
            reqDoc = hongYuClient.getReqDoc(thirdHongyuCfg, hongYuReq);
        }
        // 请求报文
        F20C08Req req = JSON.parseObject(reqDoc, F20C08Req.class);
        // 校验请求报文
        HongYuRsp validate = hongyuPullValidateLogic.validate(req);
        String apiNo = HongYuConstants.FUNC_20C08;
        final Integer id = thirdHongyuPullLogic.saveHyApiRecord(apiNo, apiNo, req, rsp, validate);
        log.info("f20C08 调用业务接口");
        return rsp;
    }

    @Override
    public R<UpdateRecordRsp> update(UpdateRecordReq updateRecordReq) {
        final Integer cnt = thirdHongyuPullLogic.updateHyApiRecord(updateRecordReq.getId(), updateRecordReq.getRspCode(), updateRecordReq.getOptResult());
        if (1 == cnt) {
            return R.ok(new UpdateRecordRsp());
        }
        return R.failed(CommonCode.FAILED);
    }
}
